<?php
$types = array();
$groups = array();
$zones = array();
$activeGroup = __('Maakond');
$activeZone = false;
$activeType = __('Tüüp');
$i = 0;
foreach ($methods as $index => $method)
{
  $types[$method['id']] = $method['name'];
  if ($activeMethod == $method['id']) {
    $activeType = $method['name'];
  }
  foreach($method['ShippingZones'] as $zone) {
    $groups[$i] = $method['name'].', '.substr($zone['ShippingZone']['name'], 0, strpos($zone['ShippingZone']['name'], ","));
    $zones[$zone['ShippingZone']['id']] = $method['name'].', '.$zone['ShippingZone']['name'];
    if($activeShippingZone == $zone['ShippingZone']['id']){
            $activeGroup = $groups[$i];
            $activeZone = $zones[$zone['ShippingZone']['id']];
    }
    $i++;
  }
}
//foreach($shippingZones as $index => $zone){
//	$groups[$index] = substr($zone['name'], 0, strpos($zone['name'], ","));
//	$zones[$zone['id']] = $zone['name'];
//
//	if($activeShippingZone == $zone['id']){
//		$activeGroup = $groups[$index];
//		$activeZone = $zones[$zone['id']];
//	}
//}
?>
<?php include_partial('homepage/customdropdown',
		array(
			'choices' => array_unique($types),
			'input' => array(
				'class' => 'grouped_zone',
				'label' => $activeType,
				'plugin_input_id' => 'type',
				'input_id' => 'type2',
				'name' => 'typeID',
				'value' => $activeMethod)
			)
	)
?>
<div class="clear"></div>
<?php include_partial('homepage/customdropdown',
		array(
			'choices' => array_unique($groups),
			'input' => array(
				'class' => 'grouped_zone',
				'label' => $activeGroup,
				'plugin_input_id' => 'foo',
				'input_id' => 'foo2',
				'name' => 'zone_group',
				'value' => $activeGroup)
			)
	)
?>
<div class="clear"></div>
<?php include_partial('homepage/customdropdown',
		array(
			'choices' => $zones,
			'input' => array(
				'class' => 'grouped_zone',
				'label' => $activeZone,
				'plugin_input_id' => 'zone',
				'input_id' => 'zone2',
				'name' => 'zoneID',
				'value' => $activeShippingZone)
			)
	)
?>
<div class="clear"></div>
<script type="text/javascript">
$(document).ready(function(){
	$( "#type" ).bind( "autocompleteselect", function(event, ui) {
		$( "#foo" ).parents('.custom-dropdown').find('.dropdownlabel').trigger('click');
		$('#foo').autocomplete( "search", ui.item.label);
	}).click(function(){
		$(this).autocomplete( "search", ""  );
	});

	$( "#foo" ).bind( "autocompleteselect", function(event, ui) {
		$( "#zone" ).parents('.custom-dropdown').find('.dropdownlabel').trigger('click');
		$('#zone').autocomplete( "search", ui.item.label);
	}).click(function(){
		$(this).autocomplete( "search", ""  );
	});

	$('#zone').click(function(){
		 if($("#foo").val()){
			 $("#zone").autocomplete("search", $("#foo").val());
		 }else if($("#foo2").val()){
			 $("#zone").autocomplete("search", $("#foo2").val());
		 }
	});

	$('#foo').click(function(){
 		 if($("#type").val()){
			 $("#foo").autocomplete("search", $("#type").val());
		 }else if($("#type2").val()){
			 $("#foo").autocomplete("search", $("#type2").val());
		 }
	});
	// triggered when dropdown updates its value
	$('#zone').parents('.custom-dropdown').bind("updated", function(){
		$(this).parents('form').trigger('changed');
	});
});
</script>

